package com.huatai.vba.quartz;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.huatai.vba.common.utils.ListUtils;
import com.huatai.vba.controller.ActionController;
import com.huatai.vba.service.VbaExtendsService;
import com.huatai.vba.service.VbaFileActionService;

@Component
public class CompareTaAndGzQuartz {

	private static final Logger logger = LoggerFactory.getLogger(ActionController.class);

	@Autowired
	private VbaExtendsService vbaExtendsService;

	@Autowired
	private VbaFileActionService vbaFileActionService;

	/**
	 * 调度间隔1个小时
	 */
	@Scheduled(fixedDelay = 1000 * 60 * 60)
	public void run() {
		String yyyyMMdd = new SimpleDateFormat("yyyyMMdd").format(new Date());
		try {
			while (isHaveTaData(yyyyMMdd)) {
				Thread.sleep(10000);
				logger.info("执行数据对比");
				vbaFileActionService.executeAction("6", "auto");
			}
		} catch (Exception ex) {
			logger.error("系统异常", ex);
		}
	}

	private boolean isHaveTaData(String yyyyMMdd) throws Exception {
		String sql = "select * from comparedata where d_date = '" + yyyyMMdd + "' ";
		return ListUtils.isEmpty(vbaExtendsService.querySql(sql));
	}

}
